package controller

import (
	"hft/Http/Models/request"
	services "hft/Http/Services"
	"hft/Http/Services/user/user_model"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type User struct{}

func (*User) SearchUser(c *gin.Context) {

	var paginate *request.SearchCluePage

	c.ShouldBindJSON(&paginate)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	list, err := services.UserService.GetUserList(paginate, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(list, c)

}

func (*User) GetUserInfo(c *gin.Context) {

	id, _ := strconv.Atoi(c.DefaultQuery("id", "0"))

	if id == 0 {
		utils.FailWithMessage("无效的id", c)
		return
	}

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.UserService.GetUserInfo(id, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)

}

// 屏蔽电话
func (*User) ShieldPhone(c *gin.Context) {
	var data *user_model.ShieldPhone
	c.ShouldBindJSON(&data)
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}
	if err := services.UserService.ShieldPhone(int(data.ID), claims.BaseClaims, data.IsShield); err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)

}

var UserController = new(User)
